package com.flowerplus.store.common.structure.presenter.compress;

import android.content.Context;
import android.net.Uri;

import com.flowerplus.store.api.ApiService;
import com.flowerplus.store.common.data.injector.PerActivity;

import java.io.File;

import javax.inject.Inject;

import rx.Subscription;


/**
 * Created by z on.
 * Des:
 * Date: 2017/3/14
 */
@PerActivity
public class CompressPresenter implements CompressContract.Presenter {
    CompressContract.View mView;
    Context mContext;
    ApiService mApiService;
    private Subscription mSubscription;
    private Subscription mSubscribeLuban;

    @Inject
    public CompressPresenter(Context context, ApiService apiService) {
        mContext = context;
        mApiService = apiService;
    }

    @Override
    public void onDestroy() {
        if (mSubscribeLuban != null) mSubscribeLuban.unsubscribe();
        if (mSubscription != null) mSubscription.unsubscribe();
         mView = null;
    }

    @Override
    public void onCreate(CompressContract.View view) {
        this.mView = view;
    }



    @Override
    public void uriToUrl(final Uri uri) {
//        mSubscription = Observable.create(new Observable.OnSubscribe<String>() {
//            @Override
//            public void call(Subscriber<? super String> subscriber) {
//                ContentResolver resolver =
//                        mContext.getContentResolver();
//                Cursor data = resolver.query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
//                if (data != null) {
//                    if (data.moveToFirst()) {
//                        int imageIndex = data.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//                        String path = data.getString(imageIndex);
//
//                        subscriber.onNext(path);
//                    }
//                    if (data != null) data.close();
//                } else {
//                    subscriber.onError(new NullPointerException("无数据"));
//                }
//                subscriber.onCompleted();
//            }
//        }).flatMap(new Func1<String, Observable<File>>() {
//            @Override
//            public Observable<File> call(String s) {
//                return Luban.get(mContext).load(new File(s)).asObservable();
//            }
//        }).subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<File>() {
//                    @Override
//                    public void onCompleted() {
//                        mView.hideLoading();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        e.printStackTrace();
//                        mView.hideLoading();
//
//                        mView.error(ExceptionHandle.handle(e));
//                    }
//
//                    @Override
//                    public void onNext(File file) {
//                        mView.compressComplete(file);
//                    }
//                });

    }

    @Override
    public void compressPhoto(final File file) {
//        final File tempFile = new File(file.getPath(), file.getName());
//        mView.showLoading();
//        mSubscribeLuban = Luban.get(mContext).load(file)
//                .asObservable()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<File>() {
//                    @Override
//                    public void onCompleted() {
//                        mView.hideLoading();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        e.printStackTrace();
//                        mView.hideLoading();
//                        mView.compressComplete(tempFile);
//                        Logger.e(ExceptionHandle.handle(e));
//                    }
//
//                    @Override
//                    public void onNext(File file) {
//                        mView.compressComplete(file);
//                    }
//                });
    }

}
